<template>
  <!-- !新增和修改的弹窗 -->
  <el-dialog
    :title="title"
    :visible.sync="dialogShow"
    @close="closeDialog"
  >
    <el-form ref="form" :model="formData" :rules="formRules" label-width="160px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:420px" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:420px" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:440px" placeholder="请选择" @focus="getEmployeeData">
          <el-option v-for="(item,index) in managerList " :key="index" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" rows="3" type="textarea" style="width:420px" />
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button @click="dialogShow = false">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </el-row>
    </el-form>

  </el-dialog>
</template>

<script>
import { getEmployeeSimpleApi } from '@/api/employee'
import { getDepartmentListApi, addDepartmentApi, getDepartmentInfoApi, editDepartmentApi } from '@/api/department'
export default {
  name: 'AddDept',
  data() {
    // 部门校验
    const checkNameRepeat = async (valid, value, cb) => {
      // 1.找到所有部门的名称
      const { depts } = await getDepartmentListApi()
      // 2.找到当前元素的兄弟
      //   console.log(depts)
      let arr = depts.filter(item => item.pid === this.formData.pid)
      //   如果在编辑状态下，排除自己
      if (this.formData.id) {
        arr = arr.filter(item => item.id !== this.formData.id)
      }
      // 3.名称查重
      const isNameRepeat = arr.some(item => item.name === value)
      isNameRepeat ? cb(new Error('名称重复了')) : cb()
    }
    // 编码校验
    const checkCodeRepeat = async(valid, value, cb) => {
      let { depts } = await getDepartmentListApi()
      if (this.formData.id) {
        depts = depts.filter(item => item.id !== this.formData.id)
      }
      const isCodeRepeat = depts.some(item => item.code === value)
      isCodeRepeat ? cb(new Error('编码重复了')) : cb()
    }
    return {
      // 弹窗控制变量
      dialogShow: false,
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      formRules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepeat }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }]
      },
      //   负责人数据
      managerList: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '新增'
    }
  },
  methods: {
    // 获取负责人数据
    async getEmployeeData() {
      this.managerList = await getEmployeeSimpleApi()
    },
    // 获取员工的简单列表
    async btnOk() {
    //   console.log(this.$refs.form)
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return console.log(error)
      }
      if (this.formData.id) {
        await editDepartmentApi(this.formData)
        this.$message.success('修改成功')
      } else {
        // 2.发出请求
        await addDepartmentApi(this.formData)
        // 3.提示成功
        this.$message.success('添加成功')
      }
      // 4.更新列表
      this.$emit('updateList')
      // 5.关闭弹窗，重置表单
      this.dialogShow = false
    },
    // 关闭弹窗
    closeDialog() {
      // 重置表单的逻辑写在这里
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      }
      // 移除校验
      this.$refs.form.clearValidate()
    },
    // 获取回显数据
    async getFormDataInfo(ID) {
      const res = await getDepartmentInfoApi(ID)
      // console.log(res)
      // 回显表单
      this.formData = res
      this.dialogShow = true
    }

  }
}
</script>

<style>

</style>
